﻿using System;
using AgencyManagerSystem.DataFetcher;
using AgencyManagerSystem.DataUpdater;
using AgencyManagerSystem.Model;

namespace AgencyManagerSystem.Core
{
    public class UserUpdaterCreator : BaseUpdaterCreator<UserModel>
    {
        UserFetcher userFetcher = new UserFetcher();
        public UserUpdater CreateUserUpdaterInstance()
        {
            UserUpdater userUpdater = new UserUpdater(userFetcher);
            userUpdater.DataUpdating += FlagAndApproveFlagValid;
            userUpdater.DataUpdating += DataNotSpace;
            userUpdater.DataUpdating += UserCodeUnique;
            userUpdater.DataDeleting += MemberExistCheck;
            userUpdater.DataDeleting += IsAdmin;
            userUpdater.DataUpdating += ReferExist;
            return userUpdater;
        }

        private void IsAdmin(BaseUpdater<UserModel> sender, DataDeletingEventArgs<UserModel> e)
        {
            if (e.Delete == null) return;
            if(e.Delete.AutoId == 1)
            {
                e.Cancel = true;
                e.Message = String.Format("系统管理员不允许删除");
                return;
            }
        }

        private void ReferExist(BaseUpdater<UserModel> sender, DataUpdatingEventArgs<UserModel> e)
        {
            if (e.Insert == null) return;
            var departmentFetcher = new DepartmentFetcher();
            var department = departmentFetcher.Get(e.Insert.Department);
            if(department==null)
            {
                e.Cancel = true;
                e.Message = String.Format("不存在部门档案{0}", e.Insert.Department);
                return;
            }
        }

        private void MemberExistCheck(BaseUpdater<UserModel> sender, DataDeletingEventArgs<UserModel> e)
        {
            if (e.Delete == null) return;
            var suppilerMemberFetcher = new SupplierMemberFetcher();
            var suppilerMember = suppilerMemberFetcher.GetByUser(e.Delete.AutoId);
            if (suppilerMember!=null)
            {
                e.Cancel = true;
                e.Message = String.Format("用户{0}已加入供应商{1}，不允许删除", e.Delete.Code, suppilerMember.Code);
                return;
            }

            var retailMemberFetcher = new RetailMemberFetcher();
            var retailMember = retailMemberFetcher.GetByUser(e.Delete.AutoId);
            if (retailMember != null)
            {
                e.Cancel = true;
                e.Message = String.Format("用户{0}已加入零售商{1}，不允许删除", e.Delete.Code, suppilerMember.Code);
                return;
            }
        }

        private void DataNotSpace(BaseUpdater<UserModel> sender, DataUpdatingEventArgs<UserModel> e)
        {
            if (e.Insert == null) return;
            if(String.IsNullOrWhiteSpace(e.Insert.Code)||String.IsNullOrWhiteSpace(e.Insert.Name) || String.IsNullOrWhiteSpace(e.Insert.Phone))
            {
                e.Cancel = true;
                e.Message = "工号、姓名与电话不能为空";
                return;
            }

        }

        private void UserCodeUnique(BaseUpdater<UserModel> sender, DataUpdatingEventArgs<UserModel> e)
        {
            if (e.Insert == null)
                return;
            var userModel = userFetcher.GetByCode(e.Insert.Code);
            if(userModel!=null&&userModel.AutoId!=e.Insert.AutoId)
            {
                e.Cancel = true;
                e.Message = String.Format("工号{0}重复",e.Insert.Code);
                return;
            }
        }
    }
}
